package travianBot;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;

import org.htmlcleaner.HtmlCleaner;
import org.htmlcleaner.TagNode;
import org.htmlcleaner.XPatherException;

public class Account {
	HashMap<String, Village> villageMap = new HashMap<String, Village>();
	public static final String NEWLINE = System.getProperty("line.separator");	
	
	/**
	 * called with the node created from the div 'side_info'. Taken from the main resource screen 'dorf1'
	 * Also setup the villages on this account object
	 * @param node
	 * @throws XPatherException 
	 */
	public HashMap<String, Village> initVillages(TagNode parentNode) throws XPatherException{
		//TODO: needs more validation
		HashMap<String, Village> currentVillageMap = new HashMap<String, Village>();
		Object[] nodeArray = parentNode.evaluateXPath("//a");
		
		TagNode node;
		String attributeValue;
		String villageName;
		String idString;
		Integer id;
		//skip the first entry as it's the wrong link
		//TODO: make this regex better so it doesn't need to skip one element
		for (int i = 1; i < nodeArray.length; i++) {
			node = (TagNode)nodeArray[i];
			attributeValue = node.getAttributeByName("href");
			villageName = node.getText().toString();
			//trim the useful bit out
			idString = attributeValue.substring(attributeValue.lastIndexOf("=")+1, attributeValue.length() );
			id =  Integer.parseInt(idString);
			Village village = new Village(id, villageName);
			currentVillageMap.put(villageName, village);
			//System.out.println("initialising village:"+id+"="+villageName);
		}
		this.villageMap = currentVillageMap;
		return currentVillageMap;
	}
	
	/**
	 * return the list of villages which have not yet been loaded
	 * @return
	 */
	public ArrayList<Village> getVillagesWhichArentLoaded(){
		ArrayList<Village> returnList = new ArrayList<Village>();
		for (Village village : villageMap.values()) {
			if(!village.isLoaded()){
				returnList.add(village);
			}
		}
		return returnList;
	}
	
	public ArrayList<Village> getVillagesUnderAttack(){
		ArrayList<Village> returnList = new ArrayList<Village>();
		for (Village village : villageMap.values()) {
			if(village.getAttackFlag()){
				returnList.add(village);
			}
		}
		return returnList;
	}
	
	public Village getVillage(String villageName){
		return villageMap.get(villageName);
	}
	
	public String printVillageCounters(){
		StringBuilder out = new StringBuilder();
		for (String key : villageMap.keySet()) {
			Village village = villageMap.get(key);
			String resourceCounterString = village.getResourceCountersString();
			String villageName = TravianBotUtil.stringPad(key," ",Village.NAME_FIELS_SIZE-key.length());
			out.append(villageName + ":"+resourceCounterString).append(NEWLINE);
		}
		return out.toString();
	}
	
	public String printCSVOutput(){
		//format date,time,village,resource,current,total,percent_storage_remaining		
		
		StringBuilder out = new StringBuilder();
		for (String key : villageMap.keySet()) {
			Village village = villageMap.get(key);
			out.append(village.getResourceCountersCSVString());
		}
		return out.toString();
	}
}
